package com.farm.shop.service.impl;

import com.farm.device.pojo.Device;
import com.farm.entity.PageResult;
import com.farm.farming.pojo.Customer;
import com.farm.shop.dao.CustDeviceMapper;
import com.farm.shop.dao.DeviceMapper;
import com.farm.shop.dao.FarmShopMapper;
import com.farm.shop.pojo.CustDevice;
import com.farm.shop.pojo.Shop;
import com.farm.shop.service.CustDeviceService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class CustDeviceServiceImpl implements CustDeviceService {
    @Autowired
    private CustDeviceMapper custDeviceMapper;

    @Autowired
    private DeviceMapper deviceMapper;
    /**
     * 分页查询
     * @param map
     * @param size
     * @param page
     * @return
     */
    @Override
    public Page<CustDevice> findPage(Map<String, Object> map, Integer size, Integer page) {
        PageHelper.startPage(size,page);
        Example example = createExample(map);
        return (Page<CustDevice>) custDeviceMapper.selectByExample(example);
    }

    /**
     * 修改运行状态
     * @param
     */
    @Override
    public void update(Integer id) {
        CustDevice custDevice = custDeviceMapper.selectByPrimaryKey(id);
        if (custDevice==null){
            throw new RuntimeException("设备不存在");
        }
        if ("1".equals(custDevice.getStatus())){
            custDevice.setStatus("0");
        }
        else if ("0".equals(custDevice.getStatus())){
            custDevice.setStatus("1");
        }
        custDeviceMapper.updateByPrimaryKeySelective(custDevice);
    }

    /**
     * 根据id来删除设备
     * @param id
     */
    @Override
    public void deleteById(Integer id) {
        CustDevice custDevice = custDeviceMapper.selectByPrimaryKey(id);
        if ("1".equals(custDevice.getStatus())){
            throw new RuntimeException("正在运行中,不能删除");
        }
        if ("1".equals(custDevice.getInstall())){
            throw new RuntimeException("设备安装了,不能删除");
        }
        custDeviceMapper.deleteByPrimaryKey(id);
    }

    @Override
    public List<CustDevice> findCustDevicesByfarmlandId(Integer farmlandId) {
        List<CustDevice> list = custDeviceMapper.findCustDevicesByfarmlandId(farmlandId);
        return list;
    }

    /**
     * 根据id查看设备详情页
     * @param did
     * @return
     */
    @Override
    public List<Device> findById(Integer did) {
        List<Device> list = custDeviceMapper.findById(did);
        return list;
    }


    /**
     * 根据客户的设备id修改安装状态以及设备位置
     * @param cdid
     * @param position
     */
    @Override
    public void updateByCustDeviceId(Integer cdid,String position) {
        CustDevice custDevice = custDeviceMapper.selectByPrimaryKey(cdid);
        custDevice.setInstall("1");
        custDevice.setPosition(position);
        custDeviceMapper.updateByPrimaryKeySelective(custDevice);
    }

    /**
     * 修改安装状态
     * @param id
     */
    @Override
    public void update2(Integer id) {
        CustDevice custDevice = custDeviceMapper.selectByPrimaryKey(id);
        if (custDevice==null){
            throw new RuntimeException("设备不存在");
        }
        if ("1".equals(custDevice.getStatus())){
            throw new RuntimeException("设备正在运行中,不能卸载");
        }
        if ("1".equals(custDevice.getInstall())){
            custDevice.setStatus("0");
        }
        else if ("0".equals(custDevice.getInstall())){
            custDevice.setStatus("1");
        }
        custDeviceMapper.updateByPrimaryKeySelective(custDevice);
    }

    /**
     * 添加设备
     * @param cid
     * @param did
     * @param code
     */
    @Override
    public void addShop(Integer cid, Integer did, String code ,String position) {

        Map<String,Object>map=new HashMap<>();
        map.put("cid",cid);
        map.put("did",did);
        map.put("code",code);
        map.put("status",0);
        map.put("install",0);
        map.put("position",position);
        custDeviceMapper.addShop(map);

    }

    private Example createExample(Map<String, Object> map) {
        Example example=new Example(CustDevice.class);
        Example.Criteria criteria = example.createCriteria();
        if (map!=null){
            if(map.get("code")!=null && !"".equals(map.get("code"))){
                criteria.andEqualTo("code",map.get("code"));
            }
            if (map.get("install")!=null && !"".equals(map.get("install"))){
                criteria.andEqualTo("install",map.get("install"));
            }
        }
        return example;
    }
}
